package com.pxst.sys.service.impl;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.pxst.entity.TaskEntity;
import com.pxst.exception.BusinessException;
import com.pxst.req.CreateTaskRsp;
import com.pxst.req.PageTaskRsp;
import com.pxst.req.UpdateTaskRsp;
import com.pxst.resp.ApiResp;
import com.pxst.resp.PageResp;
import com.pxst.sys.mapper.TaskMapper;
import com.pxst.sys.service.TaskService;
import com.pxst.utils.BuildQueryUtils;
import com.pxst.utils.PlugsUtils;
import com.pxst.utils.TaskUtil;

/**
 * @author yangxy
 * @version 创建时间：2023年8月15日 上午11:36:13
 */
@Service
public class TaskServiceImpl extends ServiceImpl<TaskMapper, TaskEntity> implements TaskService {
	@Autowired
	private TaskUtil taskUtil;
	@Autowired
	private PlugsUtils plugsUtils;

	@Override
	@SuppressWarnings("unchecked")
	public ApiResp<String> addTask(CreateTaskRsp createTaskDto) {
		// TODO Auto-generated method stub
		TaskEntity taskEntity = new TaskEntity();
		BeanUtils.copyProperties(createTaskDto, taskEntity);
		save(taskEntity);

		if (taskEntity.getIsUse() == 0) {
			taskUtil.addTask(taskEntity);
		}
		return ApiResp.sucess();
	}

	@Override
	@SuppressWarnings("unchecked")
	public ApiResp<String> updateTask(UpdateTaskRsp updateTaskDto) {
		// TODO Auto-generated method stub
		TaskEntity taskEntity = getById(updateTaskDto.getId());
		if (ObjectUtils.isEmpty(taskEntity)) {
			throw new BusinessException(String.format("定时器%s不存在", updateTaskDto.getId()));
		}

		TaskEntity one = getOne(new QueryWrapper<TaskEntity>().lambda().eq(TaskEntity::getTaskName, updateTaskDto.getTaskName())
				.ne(TaskEntity::getId, updateTaskDto.getId()));
		if(!ObjectUtils.isEmpty(one)) {
			throw new BusinessException(String.format("定时器名称%s已存在", updateTaskDto.getTaskName()));
		}

		BeanUtils.copyProperties(updateTaskDto, taskEntity);
		updateById(taskEntity);

		if (taskEntity.getIsUse() == 0) {
			taskUtil.updateTask(taskEntity);
		}
		return ApiResp.sucess();
	}

	@Override
	@SuppressWarnings("unchecked")
	public ApiResp<String> stopTask(Long id) {
		// TODO Auto-generated method stub
		TaskEntity taskEntity = getById(id);
		if (ObjectUtils.isEmpty(taskEntity)) {
			throw new BusinessException(String.format("定时器%s不存在", id));
		}
		
		taskEntity.setIsUse(1);
		updateById(taskEntity);
		taskUtil.removeTask(taskEntity.getTaskName());
		return ApiResp.sucess();
	}

	@Override
	@SuppressWarnings("unchecked")
	public ApiResp<String> stopAllTask() {
		// TODO Auto-generated method stub
		taskUtil.stopAllTask();
		return ApiResp.sucess();
	}

	@Override
	@SuppressWarnings("unchecked")
	public ApiResp<String> runTask(Long id) {
		// TODO Auto-generated method stub
		TaskEntity taskEntity = getById(id);
		if (ObjectUtils.isEmpty(taskEntity)) {
			throw new BusinessException(String.format("定时器%s不存在", id));
		}
		
		taskEntity.setIsUse(0);
		updateById(taskEntity);
		taskUtil.addTask(taskEntity);
		return ApiResp.sucess();
	}

	@Override
	@SuppressWarnings("unchecked")
	public ApiResp<PageResp<TaskEntity>> page(PageTaskRsp pageTaskDto) {
		// TODO Auto-generated method stub
		QueryWrapper<TaskEntity> build = BuildQueryUtils.build(TaskEntity.class, pageTaskDto);
		PageHelper.startPage(pageTaskDto.getPage(), pageTaskDto.getRp());
		Page<TaskEntity> page = (Page<TaskEntity>) list(build);
		return ApiResp.page(page);
	}

	@Override
	@SuppressWarnings("unchecked")
	public ApiResp<PageResp<TaskEntity>> run(Long id) {
		// TODO Auto-generated method stub
		TaskEntity taskEntity = getById(id);
		if(ObjectUtils.isEmpty(taskEntity)) {
			return ApiResp.bussError("定时器不存在");
		}
		String excuteMethod = taskEntity.getExcuteMethod();
		plugsUtils.plugsExec(taskEntity.getBeanName(),excuteMethod ,null);
		return ApiResp.sucess();
	}

}
